package com.bishe.cyh.service.Impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bishe.cyh.compont.Decompression;
import com.bishe.cyh.mapper.UploadMapper;
import com.bishe.cyh.mode.Dataji;
import com.bishe.cyh.mode.Messqge;
import com.bishe.cyh.mode.Result;
import com.bishe.cyh.mode.ReturnCode;
import com.bishe.cyh.service.UploadService;
import com.bishe.cyh.utils.ListSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: Poppin
 * @Date: 2022/1/11 21:44
 * @Version: 1.0
 */

@Service
public class UploadServiceImpl implements UploadService {

    @Value("${config.imagepath.path}")
    public String Path;



    private UploadMapper uploadMapper;
    private Decompression decompression;

    @Autowired
    public UploadServiceImpl(UploadMapper uploadMapper,Decompression decompression){
        this.uploadMapper=uploadMapper;
        this.decompression=decompression;
    }

    @Override
    public Result uploadFile(MultipartFile file) throws IOException {

        String oldName = file.getOriginalFilename();
        if (!"zip".equals(oldName.substring(oldName.lastIndexOf(".")+1))){
            return new Result<String>(ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(), "请上传zip格式");
        }
        String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."),oldName.length());
        File dest = new File(Path + newName);
        file.transferTo(dest);
        long d = System.currentTimeMillis();
        Date date = new Date();
        date.setTime(d);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Dataji dataji = new Dataji();
        dataji.setStatus(0);
        dataji.setOldname(oldName.substring(0,oldName.lastIndexOf(".")));
        dataji.setNewname(newName.substring(0,newName.lastIndexOf(".")));
        dataji.setTime(simpleDateFormat.format(date));
        dataji.setId(null);
        uploadMapper.insert(dataji);
        decompression.decompression(Path+newName,Path+newName.substring(0,newName.lastIndexOf(".")),dataji.getId());
        return new Result<Messqge>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), new Messqge("上传成功等待上传至HDFS"));
    }

    @Override
    public Result DataList() {
        List<Dataji> datajiList = uploadMapper.selectList(null);
        ListSort.DataList(datajiList);
        return new Result<List>(0,"success",datajiList);
    }

    @Override
    public Result Delete(Integer id) {
        try {
            Dataji dataji = uploadMapper.selectById(id);
            UpdateWrapper<Dataji> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("newname",dataji.getNewname())
                    .set("status",4);
            uploadMapper.update(null,updateWrapper);
            decompression.Delete(dataji.getNewname());
            if (dataji!=null){
                return new Result<Messqge>(0,"成功",new Messqge("删除提交成功"));
            }else {
                return new Result<Messqge>(-1,"失败",new Messqge("删除提交失败"));
            }
        }catch (Exception e){
            return new Result<Messqge>(-1,"失败",new Messqge("删除提交失败"));
        }
    }


}
